package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.AdminMapper;
import com.bjpowernode.pojo.Admin;
import com.bjpowernode.pojo.AdminExample;
import com.bjpowernode.service.AdminService;
import com.bjpowernode.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    // 在业务逻辑层一定有数据访问层对象
    @Autowired
    AdminMapper adminMapper;

    @Override
    public Admin login(String name, String pwd) {
        // 根据传入的的用户名到DB中查询
        // 如果有条件，一定要创建AdminExample的对象，用来封装条件
        AdminExample example = new AdminExample();
        /**
         *      条件查询
         * select * from admin where [a_name =] ['admin']
         */
        // 添加用户名a_name条件(字符串拼接)
        example.createCriteria().andANameEqualTo(name);

        List<Admin> list = adminMapper.selectByExample(example);

        // 若查询到用户对象，再进行密码的[密文]比对
        if (list.size() > 0) {
            Admin admin = list.get(0);
            String md5 = MD5Util.getMD5(pwd);
            if (md5.equals(admin.getaPass())) {
                return admin;
            }
        }
        return null;
    }
}
